package com.yutel.telecom.h2;

import java.io.ByteArrayOutputStream;
import java.io.IOException;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class H2Service {

	protected static Logger logger = LoggerFactory.getLogger(H2Service.class);
	public H2Message message;
	private boolean needBody;

	public boolean getNeedBody() {
		return needBody;
	}

	public H2Service() {
		message = new H2Message();
	}

	public H2Service(String h2type, boolean flag) {
		this();
		needBody = flag;
		message.setA4(Common.addRightSpace(h2type, 12));
	}

	// 组装包体内容
	private byte[] writeInternal() throws H2Exception {
		ByteArrayOutputStream bout = null;
		try {
			bout = new ByteArrayOutputStream();
			if (needBody)
				message.setA1(Common.addRightSpace(message.HEADLEN + 1
						+ message.getBody().length + "", 5));

			bout.write(message.getHeader().getBytes()); // 写包头

			if (needBody) // 写包体
				bout.write(message.getBody());

			bout.write(H2Message.endSymbol); // 包结束符
			return bout.toByteArray();
		} catch (IOException e) {
			throw new H2Exception("H2Exception:" + e);
		} finally {
			try {
				if (bout != null)
					bout.close();
			} catch (IOException e) {
			}
		}
	}

	public void write() throws H2Exception {
		H2Socket h2 = null;
		try {
			h2 = new H2Socket();
			logger.debug("请求数据包：");
			logger.debug(Common.bytes2hex(writeInternal()));
			h2.getOutputStream().write(writeInternal());
			byte[] receive = h2.read(message);
			pareseReceive(receive);
		} catch (IOException e) {
			throw new H2Exception("写包错误:" + e);
		} finally {
			if (h2 != null)
				h2.closeSock();
		}
	}

	private void pareseReceive(byte[] receive) {
		logger.debug("返回数据包：");
		logger.debug(Common.bytes2hex(receive));
		int index = 0;
		byte a2[] = new byte[20];
		System.arraycopy(receive, index, a2, 0, a2.length);
		message.setA2(new String(a2));
		index += a2.length;
		byte a3[] = new byte[1];
		System.arraycopy(receive, index, a3, 0, a3.length);
		message.setA3(new String(a3));
		if (a3[0] == 1) { // 成功
			index += a3.length;
			byte a4[] = new byte[12];
			System.arraycopy(receive, index, a4, 0, a4.length);
			message.setA4(new String(a4));
			index += a4.length;
			byte a5[] = new byte[20];
			System.arraycopy(receive, index, a5, 0, a5.length);
			message.setA5(new String(a5));
			index += a5.length;
			byte a6[] = new byte[1];
			System.arraycopy(receive, index, a6, 0, a6.length);
			message.setA6(new String(a6));
			index += a6.length;
			byte a7[] = new byte[6];
			System.arraycopy(receive, index, a7, 0, a7.length);
			message.setA7(new String(a7));
			index += a7.length;
			byte a8[] = new byte[8];
			System.arraycopy(receive, index, a8, 0, a8.length);
			message.setA8(new String(a8));
			index += a8.length;
			byte a9[] = new byte[5];
			System.arraycopy(receive, index, a9, 0, a9.length);
			message.setA9(new String(a9));
			index += a9.length;
			byte a10[] = new byte[1];
			System.arraycopy(receive, index, a10, 0, a10.length);
			message.setA10(new String(a10));
			index += a10.length;
			byte a11[] = new byte[5];
			System.arraycopy(receive, index, a11, 0, a11.length);
			message.setA11(new String(a11));

			if (new String(a11).equals("00000")) { // 错误码
				index += a11.length;
				int len = Integer.parseInt(message.getA1().trim());
				if (len - 1 >= message.HEADLEN) { // 去掉1位包结束符
					byte body[] = new byte[len - 1 - message.HEADLEN];
					System.arraycopy(receive, index, body, 0, body.length);
					message.setBody(body);
				}
			}

		}
	}
}